package tech.wetech.admin.modules.system.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.service.impl.BaseService;
import tech.wetech.admin.modules.system.mapper.ProjectManagementMapper;
import tech.wetech.admin.modules.system.po.ProjectManagement;
import tech.wetech.admin.modules.system.service.ProjectManagementService;
import tk.mybatis.mapper.code.Style;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lu
 * @description
 * @date 2019年07月29日 15:25
 */
@Service
public class ProjectManagementServiceImpl extends BaseService<ProjectManagement> implements ProjectManagementService {

    @Autowired
    private ProjectManagementMapper projectManagementMapper;

    @Override
    public void createProjectManagement(ProjectManagement projectManagement) {
        //获取服务器事件转成合适的格式
        Date currentTime = new Date();
        //改变输出格式（自己想要的格式）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        projectManagement.setCreateTime(currentTime);
        try {
            projectManagement.setStartTime(formatter.parse(projectManagement.getStrStartTime()));
            projectManagement.setEndTime(formatter.parse(projectManagement.getStrEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        projectManagementMapper.insertSelective(projectManagement);
    }

    @Override
    public List<ProjectManagement> selectList(ProjectManagement projectManagement, PageQuery pageQuery) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Example example = new Example(ProjectManagement.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("id DESC");
        if (pageQuery.getOrderBy() != null) {
            pageQuery.setOrderBy(StringUtil.convertByStyle(pageQuery.getOrderBy(), Style.camelhump));
        }
        if (projectManagement.getProjectName()!=null){
            String projectName="%"+projectManagement.getProjectName()+"%";
            // 创建条件对象
            criteria.andLike("projectName",projectName);
        }
        if (projectManagement.getCustomer()!=null){
            String customer="%"+projectManagement.getCustomer()+"%";
            // 创建条件对象
            criteria.andLike("customer",customer);
        }
        if (projectManagement.getHead()!=null){
            String head="%"+projectManagement.getHead()+"%";
            // 创建条件对象
            criteria.andLike("head",head);
        }
        if (projectManagement.getStrStartTime()!=null){

            Date dateStart = null;
            //String posNo=busHeart.getPosNo()+"%";
            // 创建条件对象
            try {
                dateStart = format.parse(projectManagement.getStrStartTime());
                Date datEnd = format.parse(projectManagement.getStrEndTime());
                criteria.andBetween("startTime",dateStart,datEnd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (projectManagement.getIsComplete()!=null){
            // 创建条件对象
            criteria.andEqualTo("isComplete",projectManagement.getIsComplete());
        }
        if (projectManagement.getStreStartTime()!=null){

            Date dateStart = null;
            //String posNo=busHeart.getPosNo()+"%";
            // 创建条件对象
            try {
                dateStart = format.parse(projectManagement.getStreStartTime());
                Date datEnd = format.parse(projectManagement.getStreEndTime());
                criteria.andBetween("endTime",dateStart,datEnd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return PageHelper.startPage(pageQuery)
                .doSelectPage(() -> mapper.selectByExample(example));
    }

    @Override
    public void update(ProjectManagement projectManagement) {
        //改变输出格式（自己想要的格式）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            projectManagement.setStartTime(formatter.parse(projectManagement.getStrStartTime()));
            projectManagement.setEndTime(formatter.parse(projectManagement.getStrEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        projectManagementMapper.updateByPrimaryKeySelective(projectManagement);
    }
}
